﻿	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Reflection;

#if WinRT
    using System.Linq;
    using Windows.UI.Xaml;
    using Windows.UI.Interactivity;
    using TriggerBase = Windows.UI.Interactivity.TriggerBase;
#else
    using System.Linq;
    using System.Windows;
    using System.Windows.Interactivity;
    using TriggerBase = System.Windows.Interactivity.TriggerBase;
#endif

#if !SILVERLIGHT
using System.ComponentModel;
#endif

namespace DailyFx.UI
{
	using DailyFx.Common;

    /// <summary>
    ///		Host's attached properties related to routed UI messaging .
    /// </summary>
    public static class MsgProps 
	{
        internal static readonly DependencyProperty HandlerProperty =
            DependencyProperty.RegisterAttached( "Handler", typeof(object), typeof(MsgProps), null );

        static readonly DependencyProperty MessageTriggersProperty =
            DependencyProperty.RegisterAttached( "MessageTriggers", typeof(TriggerBase[]), typeof(MsgProps), null );

        /// <summary>
        ///   Places a message handler on this element.
        /// </summary>
        public static void SetHandler ( DependencyObject d, object value) 
		{
            d.SetValue(HandlerProperty, value);
        }

        /// <summary>
        ///   Gets the message handler for this element.
        /// </summary>
        public static object GetHandler ( DependencyObject d )
		{
            return d.GetValue(HandlerProperty);
        }

        /// <summary>
        ///   A property definition representing attached triggers and messages.
        /// </summary>
        public static readonly DependencyProperty RegisterProperty = DependencyProperty.RegisterAttached
			( "Register", typeof(string), typeof(MsgProps), new PropertyMetadata(null, OnRegisterChanged) );

        /// <summary>
        ///   Sets the attached triggers and messages.
        /// </summary>
        public static void SetRegister ( DependencyObject d, string text )
		{
            d.SetValue(RegisterProperty, text);
        }

        /// <summary>
        ///   Gets the attached triggers and messages.
        /// </summary>
		public static string GetRegister ( DependencyObject d ) 
		{
            return d.GetValue(RegisterProperty) as string;
        }

        static void OnRegisterChanged ( DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
            if (e.NewValue == e.OldValue) 
			{
                return;
            }

            var messageTriggers = (TriggerBase[])d.GetValue(MessageTriggersProperty);
            var allTriggers = Interaction.GetTriggers(d);

            if (messageTriggers != null) {
                messageTriggers.Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = MessageAction.TryParseTriggerAndMessageAction ( d, e.NewValue as string).ToArray();
            newTriggers.Apply(allTriggers.Add);

            if (newTriggers.Length > 0) {
                d.SetValue(MessageTriggersProperty, newTriggers);
            }
            else {
                d.ClearValue(MessageTriggersProperty);
            }
        }
	
    }
}
